﻿using System.ComponentModel.DataAnnotations;
using MediatR;
using SheepCultivation.API.AddliCation.AddressW;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.Tip_newspaper
{
    public class LambWeaningSeachHandler : IRequestHandler<LambWeaningSeachCommon, Result>
    {
        public readonly IBaseRepository<Lamb> LambRepository;
        public readonly IBaseRepository<SheepInfo> SheepInfoRepository;
        public readonly IBaseRepository<Columns> ColumnsRepository;
        public readonly IBaseRepository<Building> BuildingRepository;
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        public LambWeaningSeachHandler(IBaseRepository<Lamb> lambRepository, IBaseRepository<SheepInfo> sheepInfoRepository, IBaseRepository<Columns> columnsRepository, IBaseRepository<Building> buildingRepository,
            IBaseRepository<BasicInformation> basicInformationRepository)
        {
            LambRepository = lambRepository;
            SheepInfoRepository = sheepInfoRepository;
            ColumnsRepository = columnsRepository;
            BuildingRepository = buildingRepository;
            BasicInformationRepository = basicInformationRepository;
        }

        public async Task<Result> Handle(LambWeaningSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                res.Code = (int)CommonEnumCode.success;
                var list = (from a in LambRepository.GetAll()
                            join b in SheepInfoRepository.GetAll()
                            on a.BreedingSheepArchiveId equals b.BreedingSheepArchiveId
                            join c in ColumnsRepository.GetAll()
                            on a.ColumnsEnter equals c.ColumnsId
                            join d in BuildingRepository.GetAll()
                            on a.BuildingsEnter equals d.BuildingId
                            join e in BasicInformationRepository.GetAll()
                            on a.BreedingSheepArchiveId equals e.BreedingSheepArchiveId
                            select new LamdDTO
                            {
                                LambId=a.LambId,
                                BreedingSheepArchiveId=e.BreedingSheepArchiveId,
                                Auricle =e.Auricle,
                                SheepAge=b.SheepAge,
                                Heavy=a.Heavy,
                                BreedingBugles=b.BreedingBugles,
                                Parity=b.Parity,
                                buildingName=d.BuildingName,
                                ColumnsName=c.ColumnsName,
                                BEnter = BuildingRepository.GetAll().Where(x => x.BuildingId == a.BuildingId).FirstOrDefault().BuildingName,
                                CEnter = ColumnsRepository.GetAll().Where(x => x.ColumnsId == a.ColumnsId).FirstOrDefault().ColumnsName,
                                WeanedDate=b.WeanedDate
                            }).ToList();
                if (!string.IsNullOrEmpty(request.WeanedDate))
                {
                    list = list.Where(x => x.WeanedDate >= DateTime.Parse(request.WeanedDate)).ToList();
                }
                if (!string.IsNullOrEmpty(request.JDate))
                {
                    list = list.Where(x => x.WeanedDate <= DateTime.Parse(request.JDate)).ToList();
                }
                if (request.BreedingSheepArchiveId > 0)
                {
                    list = list.Where(x => x.BreedingSheepArchiveId == request.BreedingSheepArchiveId).ToList();
                }
                if (!string.IsNullOrEmpty(request.BreedingBugles))
                {
                    list=list.Where(x=>x.BreedingBugles== request.BreedingBugles).ToList();
                }
                res.Code = (int)CommonEnumCode.success;
                res.Data = list;
                res.Msg = "羔羊断奶显示";
            }
            catch (Exception ex)
            {

                throw;
            }
            return res;
        }
    }

    public class LamdDTO
    {
        /// <summary>
        /// 羔羊id
        /// </summary>
        public int LambId { get; set; }
        /// <summary>
        /// 羔羊耳号
        /// </summary>
        public int BreedingSheepArchiveId { get; set; }
        public string Auricle { get; set; }
        /// <summary>
        /// 羔羊日龄
        /// </summary>
        public string SheepAge { get; set; }
        /// <summary>
        /// 断奶重
        /// </summary>
        public string Heavy { get; set; }
        /// <summary>
        /// 哺乳种母耳号
        /// </summary>
        public string BreedingBugles { get; set; }
        /// <summary>
        /// 胎次
        /// </summary>
        public int Parity { get; set; }
        /// <summary>
        /// 断奶栋舍
        /// </summary>
        public int BuildingId { get; set; }
        public string buildingName {  get; set; }

        /// <summary>
        /// 断奶栏位
        /// </summary>
        public int ColumnsId { get; set; }
        public string ColumnsName { get; set; }
        /// <summary>
        /// 转入栋舍
        /// </summary>
        public int BuildingsEnter { get; set; }
        public string BEnter { get; set; }
        /// <summary>
        /// 转入栏位
        /// </summary>
        public int ColumnsEnter { get; set; }
        public string CEnter { get; set; }
        /// <summary>
        /// 断奶日期
        /// </summary>
        public DateTime WeanedDate { get; set; }
    }
}
